package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.ReleaseGoodsActivity;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.Blog;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：ListView版，
 * 田家商品，可以对商品进行管理
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/8 14:44
 * 修改备注：
 */
public class BlogSelectAdapter2 extends BaseAdapter {
    private ArrayList<Blog> datas;
    private Context mContext;
    private OnItemClickListener clickListener;
    private int type = 1;//1不显示选择，2选择选择

    public BlogSelectAdapter2(Context context, ArrayList<Blog> datas) {
        super(context);
        this.mContext = context;
        this.datas = datas;
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    @Override
    public boolean isEmpty() {
        int size = datas == null ? 0 : datas.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_blog_select, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        } else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.iv_select)
        ImageView ivSelect;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_size)
        TextView tvSize;
        @Bind(R.id.tv_cash)
        TextView tvCash;
        @Bind(R.id.tv_food_stamp)
        TextView tvFoodStamp;
        @Bind(R.id.tv_editor)
        TextView tvEditor;
        int position = -1;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && position != -1) {
                        clickListener.onClickListener(position);
                    }
                }
            });
            tvEditor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position != -1) {
                        Intent itNew = new Intent(mContext, ReleaseGoodsActivity.class);
                        itNew.putExtra("blog_id", datas.get(position).getId());
                        mContext.startActivity(itNew);
                    }
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            Blog blog = datas.get(position);
            ImageUtils.loadRadiusImage(mContext, blog.getImgurl(), ivBkg, 10);
            tvName.setText(blog.getName());
            tvPrice.setText("¥" + uuUtils.formatAfterDot2(blog.getPrice()));
            tvSize.setText("规格:" + blog.getUnit());
            double price = Double.parseDouble(blog.getPrice());
            double score = Double.parseDouble(blog.getScore());
            if (price-score < 0) {
                tvCash.setText("¥0.00"+"+");
            }else {
                tvCash.setText("¥"+uuUtils.formatAfterDot2(price-score+"")+"+");
            }
            tvFoodStamp.setText(blog.getScore()+"粮票");
            tvEditor.setVisibility(View.VISIBLE);
            if (type == 2) {
                ivSelect.setVisibility(View.VISIBLE);
                if (blog.isCheck())
                    ivSelect.setImageResource(R.drawable.select_y_orange_3);
                else
                    ivSelect.setImageResource(R.drawable.select_n_trans);
            }else
                ivSelect.setVisibility(View.INVISIBLE);
        }
    }
}
